﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cls.u_Models
{
    public partial class EmpstatMgr : System.Web.UI.Page
    {
        static String UserName = "";
        static String UserInOrgCode = "";
        static String UserInOrgName = "";
        static Boolean UserInZB = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            UserName = User.Identity.Name;
            UserInOrgCode = GetOrgCodeByUserName(UserName);
            UserInOrgName = GetOrgNameByUserName(UserName);

            if (!Page.IsPostBack)
            { }

            Label_UserName.Text = UserName;
            Label_OrgName.Text = UserInOrgCode + "-" + UserInOrgName;

            if (UserInOrgCode.Trim().Equals("ZB"))
            {
                Label_NavLink.Text = "<ol>";

                //2014-11-19修改，总部查看，门店上传
                //Label_NavLink.Text += "<li><a href=\"../u_ExcelFiles/门店人员统计表（模版）.xlsx\">下载《门店人员统计表（模版）》</a></li>";
                //Label_NavLink.Text += "<li><a href=\"FileUpload.aspx?App=Empstat&OrgCode=ZB\">上传门店人员统计表</a></li>";

                Label_NavLink.Text += "<li><a href=\"ShowEmpstat.aspx\">查看门店人员统计电子表单</a></li>";
                //Label_NavLink.Text += "<li><a href=\"BackorderUserMgr.aspx\">用户管理</a></li>";
                Label_NavLink.Text += "</ol>";
            }
            else
            {
                Label_NavLink.Text = "<ol>";
                
                //2014-11-19修改，总部查看，门店上传
                Label_NavLink.Text += "<li><a href=\"../u_ExcelFiles/门店人员统计表（模版）.xlsx\">下载《门店人员统计表（模版）》</a></li>";
                Label_NavLink.Text += "<li><a href=\"FileUpload.aspx?App=Empstat&OrgCode=" + UserInOrgCode + "\">上传门店人员统计表</a></li>";
                Label_NavLink.Text += "<li><a href=\"ShowEmpstat.aspx\">门店人员统计表电子表单</a></li>";
                Label_NavLink.Text += "</ol>";
            }
        }

        protected String GetOrgCodeByUserName(String UserName)
        {
            String OrgCode = "";

            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=cls_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA");
            SqlConnection.Open();

            SqlCommand SelectCommand = new SqlCommand("SELECT OrgCode FROM u_cls_Org WHERE Id = (SELECT OrgId FROM u_cls_UserInOrg WHERE UserName = @UserName)", SqlConnection);

            SelectCommand.Parameters.Add("@UserName", SqlDbType.NVarChar,4);
            SelectCommand.Parameters["@UserName"].Value = UserName;

            SqlDataReader OrgCodeReader = SelectCommand.ExecuteReader();

            while (OrgCodeReader.Read())
            {
                //还是分开取得比较好，后面还有单独用到OrgCode的地方......
                //OrgCode = ResReader["OrgCode"].ToString() + "-" + ResReader["OrgName"].ToString();  
                OrgCode = OrgCodeReader["OrgCode"].ToString();
            }

            if (OrgCode.Equals("ZB"))
            {
                UserInZB = true;
            }

            OrgCodeReader.Close();
            SqlConnection.Close();

            return OrgCode;
        }

        protected String GetOrgNameByUserName(String UserName)
        {
            String OrgName = "";

            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=cls_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA");
            SqlConnection.Open();

            SqlCommand SelectCommand = new SqlCommand("SELECT OrgName FROM u_cls_Org WHERE Id = (SELECT OrgId FROM u_cls_UserInOrg WHERE UserName = @UserName)", SqlConnection);

            SelectCommand.Parameters.Add("@UserName", SqlDbType.NVarChar, 4);
            SelectCommand.Parameters["@UserName"].Value = UserName;

            SqlDataReader ResReader = SelectCommand.ExecuteReader();

            while (ResReader.Read())
            {
                //还是分开取得比较好，后面还有单独用到OrgCode的地方......
                //OrgCode = ResReader["OrgCode"].ToString() + "-" + ResReader["OrgName"].ToString();  
                OrgName = ResReader["OrgName"].ToString();
            }

            ResReader.Close();
            SqlConnection.Close();

            return OrgName;
        }
    }
}